package com.laiyw.monitor.utils;

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @Description:
 * @Auther: Laiyw
 * @Date: 2018/10/5 12:34
 */
public class ScanUtils {

    private static final String CLASS_SUFFIX = ".class";
    private static Set<Class<?>> classPaths;

    public static void main(String[] args) {
        scan("com.laiyw.monitor");
        System.out.println(classPaths.size());
        Iterator iterator = classPaths.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    public static Set<Class<?>> scan(String packageName) {
        packageName = packageName.replace(".", File.separator);
        try {
            classPaths = new HashSet<>();
            Enumeration<URL> urlEnumeration = Thread.currentThread().getContextClassLoader().getResources(packageName);
            URL url;
            while (urlEnumeration.hasMoreElements()) {
                url = urlEnumeration.nextElement();
                if ("file".equals(url.getProtocol())) {
                    searchForFile(new File(URLDecoder.decode(url.getPath(), "UTF-8")), packageName.replace(File.separator, "."), true);
                } else if ("jar".equals(url.getProtocol())) {
                    searchForJar(url);
                }
            }
            return classPaths;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static void searchForJar(URL url) throws ClassNotFoundException, IOException {
        JarURLConnection connection = (JarURLConnection) url.openConnection();
        if (connection != null) {
            JarFile jarFile = connection.getJarFile();
            if (null != jarFile) {
                Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries();
                JarEntry entry;
                String jarEntryName;
                while (jarEntryEnumeration.hasMoreElements()) {
                    entry = jarEntryEnumeration.nextElement();
                    jarEntryName = entry.getName();
                    if (jarEntryName.endsWith(CLASS_SUFFIX)) {
                        jarEntryName = jarEntryName.replace(CLASS_SUFFIX, "").replace("/", ".");
                        Class clazz = Class.forName(jarEntryName);
                        classPaths.add(clazz);
                    }
                }
            }
        }
    }

    private static void searchForFile(File file, String packageName, boolean flag) {
        if (file.isDirectory()) {
            if (!flag) {
                packageName = packageName + "." + file.getName();
            }

            for (File f : file.listFiles()) {
                searchForFile(f, packageName, false);
            }
        } else {
            if (file.getName().endsWith(CLASS_SUFFIX)) {
                try {
                    Class<?> clazz = Class.forName(packageName + "." + file.getName().replace(CLASS_SUFFIX, ""));
                    classPaths.add(clazz);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
